﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireRecordConfigBLL
    {
        #region 构造单例
        private FireRecordConfigBLL() { }
        private static FireRecordConfigBLL _instance;
        public static FireRecordConfigBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireRecordConfigBLL());
            }
        }
        #endregion

        public List<Firerecordconfig> Get(int recordConfigId,int roleLeveId,int recordType)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Firerecordconfig select a;

                if (recordConfigId > 0)
                {
                    linq = linq.Where(p => p.RecordConfigId == recordConfigId);
                }
                if (roleLeveId > 0)
                {
                    linq = linq.Where(p=>p.RoleLevelId== roleLeveId);  
                }
                if (recordType > 0)
                {
                    linq = linq.Where(p => p.RecordType == recordType);
                }
                return linq.ToList();
            }
        }


        public int Add(Firerecordconfig info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist= ef.Firerecordconfig.Where(p => p.RoleLevelId == info.RoleLevelId && p.RecordType == info.RecordType).FirstOrDefault();
                if (exist != null)
                {
                    exist.SubRecordTypes = info.SubRecordTypes;
                    exist.Sort = info.Sort;
                    ef.Firerecordconfig.Add(exist);
                    ef.Entry<Firerecordconfig>(exist).State = EntityState.Modified;
                    ef.SaveChanges();
                }
                else 
                {
                    ef.Firerecordconfig.Add(info);
                    ef.Entry<Firerecordconfig>(info).State = EntityState.Added;
                    ef.SaveChanges();
                }
            }
            return 1;
        }
    }
}
